/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fiap.servlet;

import br.com.fiap.dao.UsuarioDAO;
import br.com.fiap.util.Role;
import br.com.fiap.vo.UsuarioVO;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Fabianoss
 */
public class LoginServlet extends HttpServlet {

   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
   
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String usuario = request.getParameter("usuario");
        String senha = request.getParameter("senha");
        String paginaRetorno = "index.jsp";
        String msg = "";
        
        UsuarioDAO dao = new UsuarioDAO();
        UsuarioVO vo = dao.getUsuarioVOByLogin(usuario);
        
        if(vo != null ){
            if(vo.getSenha().equals(senha)){
                HttpSession session = request.getSession();
                session.setAttribute("usuario", vo);

                if(vo.getRole().equals(Role.ADMIN.getRole())){
                    paginaRetorno = "./paginas/menuAdm.jsp";
                }else if(vo.getRole().equals(Role.USER.getRole())){
                    paginaRetorno = "./paginas/menuAluno.jsp";
                }else{
                    msg="Contate o Adominstrador usuário sem acesso!";
                    paginaRetorno = paginaRetorno+"?msg="+msg; 
                }
            }else{
                msg="senha incorreta!";
                paginaRetorno = paginaRetorno+"?msg="+msg;
            }
        }else{
            msg = "usuário inexistente!";
            paginaRetorno = paginaRetorno+"?msg="+msg;
        }
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(paginaRetorno);  
        dispatcher.forward(request, response); 

                
   
    }

}
